<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增前置模板')" />
	<th:block th:include="include :: ztree-css" />
    <link rel="stylesheet" th:href="@{/css/myselect.css}"/>
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-template-add">
			<div class="form-group">
				<label class="col-sm-3 control-label ">模板名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="tempName" id="tempName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">事项模板</label>
				<div class="col-sm-8">
					<div id="matterTypeTrees" class="ztree"></div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: ztree-js" />
	<script type="text/javascript">
	    $(function() {
			var url = ctx + "linkong/template/treeData";
			var options = {
				id: "matterTypeTrees",
		        url: url,
		        check: { enable: true },
		        expandLevel: 3,
		    };
			$.tree.init(options);
		});
	    $("#form-matter-add").validate({
            focusCleanup: true
        });
		
	    function uniqer(arr) {
			return Array.from(new Set(arr));
		}
	    
	    function submitHandler() {
	        if ($.validate.form()) {
	        	var tempName = $("input[name='tempName']").val();
				var treeObj=$.fn.zTree.getZTreeObj("matterTypeTrees");
				var nodes=treeObj.getCheckedNodes(true);
				var matterIds="";
				var matterNames="";
				for(var i=0;i<nodes.length;i++){
					if (nodes[i].text == "事项"){
						matterIds += nodes[i].id + ",";
						matterNames += nodes[i].name + ",";
					}
				}
				matterIds = matterIds.substring(0,matterIds.length-1);
				matterNames = matterNames.substring(0,matterNames.length-1);
				$.ajax({
					cache : true,
					type : "POST",
					url : ctx + "linkong/template/add",
					data : {
						"tempName": tempName,
						"matterIds": matterIds,
						"matterNames":matterNames
					},
					async : false,
					error : function(request) {
						$.modal.alertError("系统错误");
					},
					success : function(data) {
						$.operate.successCallback(data);
					}
				});
	        }
	    }
	
	</script>
</body>
</html>
